package com.mall4j.cloud.product.service.impl;

import com.mall4j.cloud.api.product.bo.EsAttrBO;
import com.mall4j.cloud.api.product.bo.EsProductBO;
import com.mall4j.cloud.api.product.vo.*;
import com.mall4j.cloud.product.dto.SkuDTO;
import com.mall4j.cloud.product.dto.SpuAttrValueDTO;
import com.mall4j.cloud.product.dto.SpuDTO;
import com.mall4j.cloud.product.dto.SpuSkuAttrValueDTO;
import com.mall4j.cloud.product.mapper.*;
import com.mall4j.cloud.product.service.ProductService;
import com.mall4j.cloud.product.vo.SpuDetailVO;
import com.mall4j.cloud.product.vo.SpuExtensionVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品
 *
 * @author JCM
 * @date 2021-04-24 21:02:00
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private SpuMapper spuMapper;


    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private SpuAttrValueMapper spuAttrValueMapper;

    @Autowired
    private SpuDetailMapper spuDetailMapper;

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private SpuSkuAttrValueMapper spuSkuAttrValueMapper;

    @Autowired
    private SpuExtensionMapper spuExtensionMapper;

    @Autowired
    private MapperFacade mapperFacade;


    @Override
    public EsProductBO getBySpuId(Long spuId) {
        SpuVO spuVO = spuMapper.getBySpuId(spuId);
        setSpuVO(spuVO);
        EsProductBO esProductBO = mapperFacade.map(spuVO, EsProductBO.class);
        esProductBO.setSpuName(spuVO.getName());
        esProductBO.setStock(spuVO.getTotalStock());
        if(spuVO.getTotalStock() > 0){
            esProductBO.setHasStock(true);
        }else {
            esProductBO.setHasStock(false);
        }
        esProductBO.setBrandId(Integer.parseInt(spuVO.getBrandId()+""));
        esProductBO.setBrandName(spuVO.getBrand().getName());
        esProductBO.setBrandImg(spuVO.getBrand().getImgUrl());
        List<SpuAttrValueVO> spuAttrValues = spuVO.getSpuAttrValues();
        List<EsAttrBO> esAttrBOS = mapperFacade.mapAsList(spuAttrValues, EsAttrBO.class);
        esProductBO.setAttrs(esAttrBOS);

        CategoryVO platPrimaryCategory = categoryMapper.getByCategoryId(spuVO.getCategoryId());
        CategoryVO shopCategory = categoryMapper.getByCategoryId(spuVO.getShopCategoryId());
        esProductBO.setCategoryName(shopCategory.getName());
        CategoryVO shopPrimaryCategory = categoryMapper.getByCategoryId(shopCategory.getParentId());
        esProductBO.setShopPrimaryCategoryId(shopPrimaryCategory.getCategoryId());
        esProductBO.setShopPrimaryCategoryName(shopPrimaryCategory.getName());
        esProductBO.setShopSecondaryCategoryId(shopCategory.getCategoryId());
        esProductBO.setShopSecondaryCategoryName(shopCategory.getName());
        CategoryVO secondaryCategory = categoryMapper.getByCategoryId(platPrimaryCategory.getParentId());
        esProductBO.setSecondaryCategoryId(secondaryCategory.getCategoryId());
        esProductBO.setSecondaryCategoryName(secondaryCategory.getName());
        CategoryVO primaryCategory = categoryMapper.getByCategoryId(secondaryCategory.getParentId());
        esProductBO.setPrimaryCategoryId(primaryCategory.getCategoryId());
        esProductBO.setPrimaryCategoryName(primaryCategory.getName());
        return esProductBO;
    }

    @Override
    public List<Long> getSpuIdsByShopCategoryIds(List<Long> shopCategoryIds) {
        List<Long> spuIds = new ArrayList<>();
        for (Long shopCategoryId : shopCategoryIds){
            SpuDTO spuDTO = new SpuDTO();
            spuDTO.setShopCategoryId(shopCategoryId);
            List<SpuVO> spuVOS = spuMapper.list(spuDTO);
            for (SpuVO spuVO : spuVOS){
                spuIds.add(spuVO.getSpuId());
            }
        }
        return spuIds;
    }

    @Override
    public List<Long> getSpuIdsByCategoryIds(List<Long> categoryIds) {
        List<Long> spuIds = new ArrayList<>();
        for (Long categoryId : categoryIds){
            SpuDTO spuDTO = new SpuDTO();
            spuDTO.setCategoryId(categoryId);
            List<SpuVO> spuVOS = spuMapper.list(spuDTO);
            for (SpuVO spuVO : spuVOS){
                spuIds.add(spuVO.getSpuId());
            }
        }
        return spuIds;
    }

    @Override
    public List<Long> getSpuIdsByBrandId(Long brandId) {
        List<Long> spuIds = new ArrayList<>();
        SpuDTO spuDTO = new SpuDTO();
        spuDTO.setBrandId(brandId);
        List<SpuVO> spuVOS = spuMapper.list(spuDTO);
        for (SpuVO spuVO : spuVOS){
            spuIds.add(spuVO.getSpuId());
        }
        return spuIds;
    }

    @Override
    public List<Long> getSpuIdsByShopId(Long shopId) {
        List<Long> spuIds = new ArrayList<>();
        SpuDTO spuDTO = new SpuDTO();
        spuDTO.setShopId(shopId);
        List<SpuVO> spuVOS = spuMapper.list(spuDTO);
        for (SpuVO spuVO : spuVOS){
            spuIds.add(spuVO.getSpuId());
        }
        return spuIds;
    }

    private void setSpuVO(SpuVO spuVO){
        //获取商品规格属性关联信息
        SpuAttrValueDTO spuAttrValueDTO = new SpuAttrValueDTO();
        spuAttrValueDTO.setSpuId(spuVO.getSpuId());
        List<SpuAttrValueVO> spuAttrValueVOS = spuAttrValueMapper.list(spuAttrValueDTO);
        spuVO.setSpuAttrValues(spuAttrValueVOS);

        //获取品牌信息
        Long brandId = spuVO.getBrandId();
        BrandVO brandVO = brandMapper.getByBrandId(brandId);
        spuVO.setBrand(brandVO);


        //获取商品详情
        SpuDetailVO spuDetailVO = spuDetailMapper.getBySpuId(spuVO.getSpuId());
        spuVO.setDetail(spuDetailVO.getDetail());

        //获取sku
        SkuDTO skuDTO = new SkuDTO();
        skuDTO.setSpuId(spuVO.getSpuId());
        List<SkuVO> skuVOS = skuMapper.list(skuDTO);


        for (SkuVO skuVO : skuVOS){
            SpuSkuAttrValueDTO spuSkuAttrValueDTO = new SpuSkuAttrValueDTO();
            spuSkuAttrValueDTO.setSpuId(spuVO.getSpuId());
            spuSkuAttrValueDTO.setSkuId(skuVO.getSkuId());
            List<SpuSkuAttrValueVO> spuSkuAttrValueVOS = spuSkuAttrValueMapper.list(spuSkuAttrValueDTO);
            skuVO.setSpuSkuAttrValues(spuSkuAttrValueVOS);

        }

        spuVO.setSkus(skuVOS);

        SpuExtensionVO spuExtensionVO = spuExtensionMapper.getBySpuId(spuVO.getSpuId());
        spuVO.setSaleNum(spuExtensionVO.getSaleNum());
        spuVO.setTotalStock(spuExtensionVO.getActualStock());

    }

}
